<template>
  <el-dialog :title="formData.modelUuid ? '修改' : '添加'" :model-value="dialog" @close="close">
    <el-form :model="formData" label-width="auto" :rules="rules" ref="uploadForm" style="padding: 0 20px" class="Dform">
      <el-form-item label="品牌：" prop="brandUuid">
        <el-select v-model="formData.brandUuid" @change="getSeriesList(true)" filterable clearable style="width: 100%"
          placeholder="请选择品牌">
          <el-option v-for="item in brandList" :key="item.brandUuid" :label="item.brandName" :value="item.brandUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车系：" prop="seriesUuid">
        <el-select v-model="formData.seriesUuid" filterable clearable style="width: 100%" placeholder="请选择车系">
          <el-option v-for="item in seriesList" :key="item.seriesUuid" :label="item.seriesName"
            :value="item.seriesUuid" />
        </el-select>
      </el-form-item>
      <el-form-item label="车型名称：" prop="modelName">
        <el-input v-model="formData.modelName" />
      </el-form-item>
      <el-form-item label="能源类型：" prop="energyType">
        <el-select v-model="formData.energyType" filterable clearable style="width: 100%" placeholder="请选择能源类型">
          <el-option v-for="item in [...energyTypeMap].map(([value, label]) => ({ value, label }))" :key="item.value"
            :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="车型类型：" prop="modelType">
        <el-select v-model="formData.modelType" filterable clearable style="width: 100%" placeholder="请选择车型类型">
          <el-option v-for="item in [...modelTypeMap].map(([value, label]) => ({ value, label }))" :key="item.value"
            :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="发动机排量：" prop="engineCapacity">
        <el-input v-model="formData.engineCapacity" />
      </el-form-item>
      <el-form-item label="轴距：" prop="wheelbase">
        <el-input v-model="formData.wheelbase" />
      </el-form-item>
      <!-- 车型图片 -->
      <!-- <el-form-item label="车型图片：" prop="logUrl">
        <upload-file v-model="formData.logUrl" :limit="1" :fileType="0" :fileSize="1" :suffixStr="'仅支持 jpg、jpeg、png'"
          tipLabel="" />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" @click="onSubmit()">提交</el-button>
        <el-button v-if="!formData.modelUuid" @click="resetForm()">重置</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script lang="ts">
import { defineComponent, ref, PropType, watch, reactive } from 'vue'
import { ElMessage } from 'element-plus'
import { updateModel, saveModel, brandQueryAll, seriesQuery } from '@/api/vehicle'
import { energyTypeMap, modelTypeMap } from '@/config/constants'
import uploadFile from '@/components/upload-file/index.vue'


export default defineComponent({
  components: {
    uploadFile,
  },
  props: {
    dialog: {
      type: Boolean,
      default: false,
    },
    currentForm: {
      type: Object,
      default: () => { },
    },
    selectMenuList: {
      type: Array as PropType<{ title: string; id: string | number }[]>,
      default: () => [],
    },
  },
  emits: ['update:currentForm', 'update:dialog', 'refresh'],
  setup(props, context) {
    const formData = reactive(props.currentForm)
    const uploadForm = ref<any>(null)
    const brandList = ref<any[]>([])
    const seriesList = ref<any[]>([])

    watch(() => props.dialog,
      (val) => {
        Object.assign(formData, props.currentForm)
      }
    )


    const close = () => {
      context.emit('update:dialog', false)
      context.emit('refresh', { ...formData })
      context.emit('update:currentForm', {})
    }

    const onSubmit = () => {
      uploadForm.value.validate((valid: boolean) => {
        if (valid) {
          if (formData.modelUuid) {
            updateModel(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('修改成功')
                close()
              }
            })
          } else {
            saveModel(formData).then((res: any) => {
              if (res.code == 200) {
                ElMessage.success('添加成功')
                close()
              }
            })
          }

        } else {
          ElMessage({
            message: '验证失败',
            type: 'error',
            duration: 1000,
          })
          return false
        }
      })
    }

    const resetForm = () => {
      uploadForm.value.resetFields()
      context.emit('update:currentForm', {})
    }

    const getSeriesList = (flag = false) => {
      if (flag) {
        formData.seriesUuid = '';
      }
      seriesQuery({ brandUuid: formData.brandUuid, pageNum: 1, pageSize: 1000 }).then((res: any) => {
        seriesList.value = res.data.list || []
      })
    }

    onMounted(() => {
      brandQueryAll().then((res: any) => {
        brandList.value = res.data || []
      })
      getSeriesList()
    })

    return {
      uploadForm,
      formData,
      brandList,
      seriesList,
      energyTypeMap,
      modelTypeMap,
      close,
      resetForm,
      onSubmit,
      getSeriesList,
      rules: {
        brandUuid: [
          { required: true, message: '请选择品牌', trigger: 'change' }
        ],
        seriesUuid: [
          { required: true, message: '请选择车系', trigger: 'change' }
        ],
        modelName: [
          { required: true, message: '请输入车型名称', trigger: 'blur' }
        ],
        energyType: [
          { required: true, message: '请选择能源类型', trigger: 'change' }
        ],
        modelType: [
          { required: true, message: '请选择车型类型', trigger: 'change' }
        ],
        // logUrl: [
        //   { required: true, message: '请上传车型图片', trigger: 'change' }
        // ],
      },
    }
  },
})
</script>

<style></style>
